<template>
  <div>
    <sys-dialog
      :title="addDialog.title"
      :height="addDialog.height"
      :width="addDialog.width"
      :visible="addDialog.visible"
      @onClose="onClose"
      @onConfirm="onConfirm">
      <div slot="content">
        <el-form
          :model="addModel"
          ref="addForm"
          :rules="rules"
          label-width="80px"
          :inline="true"
          size="small">
          <el-form-item prop="deptName" label="所属部门">
            <el-input
              @click.native="selectDept"
              v-model="addModel.deptName"></el-input>
          </el-form-item>
          <el-form-item prop="loginName" label="姓名">
            <el-input v-model="addModel.loginName"></el-input>
          </el-form-item>
          <el-form-item prop="mobile" label="电话">
            <el-input v-model="addModel.mobile"></el-input>
          </el-form-item>
          <el-form-item label="昵称">
            <el-input v-model="addModel.nickName"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="addModel.email"></el-input>
          </el-form-item>
          <el-form-item prop="username" label="登录名">
            <el-input v-model="addModel.username"></el-input>
          </el-form-item>
          <el-form-item prop="password" label="密码">
            <el-input type="password" v-model="addModel.password"></el-input>
          </el-form-item>
          <el-form-item prop="sex" label="性别">
            <el-radio-group v-model="addModel.sex">
              <el-radio :label="0">男</el-radio>
              <el-radio :label="1">女</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
    </sys-dialog>
    <parent-vue ref="parentRef" @onConfirm="onParentConfirm"></parent-vue>
  </div>
</template>

<script>
import SysDialog from '@/components/system/SysDialog'
import parentVue from './parent.vue'
export default {
  components: {
    SysDialog,
    parentVue
  },
  data() {
    return {
      //表单验证规则
      rules: {
        deptName: [
          {
            required: true,
            trigger: 'change',
            message: '请选择所属部门'
          }
        ],
        loginName: [
          {
            required: true,
            trigger: 'change',
            message: '请填写姓名'
          }
        ],
        mobile: [
          {
            required: true,
            trigger: 'change',
            message: '请填写电话号码'
          }
        ],
        username: [
          {
            required: true,
            trigger: 'change',
            message: '请填写登录名'
          }
        ],
        password: [
          {
            required: true,
            trigger: 'change',
            message: '请填写登录密码'
          }
        ],
        sex: [
          {
            required: true,
            trigger: 'change',
            message: '请选择性别'
          }
        ]
      },
      //新增或编辑表单数据域
      addModel: {
        id: '',
        deptId: '',
        editType: '', //0新增 1：编辑
        deptName: '',
        email: '',
        loginName: '',
        mobile: '',
        nickName: '',
        password: '',
        username: '',
        sex: ''
      },
      //新增或编辑弹框属性
      addDialog: {
        title: '',
        height: 230,
        width: 610,
        visible: false
      }
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    show(type, val) {
      this.$resetForm('addForm', this.addModel)
      if (type === '0') {
        this.addDialog.title = '新增用户'
      } else {
        this.addDialog.title = '编辑用户'
        this.$objCoppy(val, this.addModel)
      }
      this.addDialog.visible = true
      this.addModel.editType = type
    },
    // 新增或编辑确认事件
    onConfirm() {
      this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          this.$emit('onConfirm', this.addModel)
          this.addDialog.visible = false
        }
      })
    },
    //新增或编辑弹框取消事件
    onClose() {
      this.$resetForm('addForm', this.addModel)
      this.addDialog.visible = false
    },
    //选择上级部门
    async selectDept() {
      console.log('选择上级部门')
      this.$refs.parentRef.show()
    },
    onParentConfirm(val) {
      this.addModel.deptId = val.id
      this.addModel.deptName = val.name
    }
  }
}
</script>
<style lang="sass" scoped></style>
